﻿<#assign classNameLower = className?uncap_first>
package ${packageName};

import org.apache.log4j.Logger;

import com.shareinfo.TBase;
import com.shareinfo.TGlobal;
import com.shareinfo.TOperator;
import com.shareinfo.smbase;
import com.shareinfo.dbc.TResultSet;

public class ${className}ManageOper extends TBase{
	private static Logger log = Logger.getLogger(${className}ManageOper.class);
	private String dstdb = "";
	
	public ${className}ManageOper(TOperator op) {
		super(op);
	}
	
	/**
	 * 查询所有的${comments}<br>
	 * @param ${classNameLower}Bean
	 * @param currentPageNumber
	 * @param everyPageTotal
	 * @return
	 */
	public TResultSet query${className}(${className}Bean ${classNameLower}Bean, int currentPageNumber,int everyPageTotal){
		TResultSet rs = null;
		String sql = " select * from ${tableName} where 1=1 ";
		try{
			<#list table.columns as column>
			if(!smbase.isEmpty(${classNameLower}Bean.get${column.classColumnName?cap_first}())){
				sql += " and ${column.sqlColumnName} = '" + ${classNameLower}Bean.get${column.classColumnName?cap_first}() + "'";
			}
			</#list>
			rs = TGlobal.g_dbcon.queryRecordPage(sql, currentPageNumber, everyPageTotal);
			if(log.isDebugEnabled()){
				log.debug("查询${comments}信息：" + sql);
			}
		}catch(Exception e){
			e.printStackTrace();
			TGlobal.g_log.WriteLogError("查询${comments}信息异常("+e.getMessage()+"):"+sql);
		}
		return rs;
	}
	

	/**
	 * 通过id获取${comments}<br>
	 * @param ${classNameLower}Bean
	 * @param currentPageNumber
	 * @param everyPageTotal
	 * @return
	 */
	public ${className}Bean query${className}ById(${table.keyJavaType} id){
		String sql = "select * from ${tableName} where 1=1";
		<#if table.keyJavaType="java.util.HashMap">
			<#list table.keyColumns as column>
				sql+=" and ${column.sqlColumnName}="+id.get("${column.classColumnName}");
			</#list>
		<#else>
			<#list table.keyColumns as column>
				sql+=" and ${column.sqlColumnName}="+id;
			</#list>
		</#if>
		${className}Bean ${classNameLower}Bean=new ${className}Bean();
		TResultSet rs = null;
		try{
			rs = TGlobal.g_dbcon.execQuery(sql);
			if(rs != null && rs.next()){
				<#list table.columns as column>
					${classNameLower}Bean.set${column.classColumnName?cap_first}(rs.getString("${column.sqlColumnName}"));
				</#list>
			}
			TGlobal.g_log.WriteLogDebug("通过ID查询${comments}信息:"+sql);
		}catch(Exception e){
			e.printStackTrace();
			TGlobal.g_log.WriteLogError("通过ID查询${comments}信息异常("+e.getMessage()+"):"+sql);
		}finally{
			if(rs != null){
				rs.close();
			}
			rs = null;
		}
		return ${classNameLower}Bean;
	}
	
	/**
	 * 增加${comments}<br>
	 * @param ${classNameLower}Bean
	 * @param currentPageNumber
	 * @param everyPageTotal
	 * @return
	 */
	public int insert${className}(${className}Bean ${classNameLower}Bean){
		int result = -1;
		String sql = " insert into ${tableName}(";
		<#list table.columns as column>
			sql+="${column.sqlColumnName}<#if column_has_next>,</#if>";
		</#list>
		sql+=") VALUES(";
			<#list table.columns as column>
				sql+=${classNameLower}Bean.get${column.classColumnName?cap_first}()<#if column_has_next>+","</#if>;
			</#list>
		sql+=")";
		try {
			result = TGlobal.g_dbcon.execUpdate(sql.toString());
			TGlobal.g_log.WriteLogDebug("新增${comments}信息:" + sql);
		}catch (Exception e) {
			e.printStackTrace();
			TGlobal.g_log.WriteLogError("新增${comments}信息异常(" + e.getMessage() + "):" + sql);
		} 
		return result;
	}
	
	/**
	 * 删除${comments}<br>
	 */
	public int delete${className}ById(${table.keyJavaType} id){
		int result = -1;
		String sql = " update ${tableName} set fsyncflag=2 where 1=1";
		<#if table.keyJavaType="java.util.HashMap">
		<#list table.keyColumns as column>
			sql+=" and ${column.sqlColumnName}="+id.get("${column.classColumnName}");
		</#list>
		<#else>
			<#list table.keyColumns as column>
				sql+=" and ${column.sqlColumnName}="+id;
			</#list>
		</#if>
		try {
			result = TGlobal.g_dbcon.execUpdate(sql.toString());
			TGlobal.g_log.WriteLogDebug("删除${comments}信息:" + sql);
		}catch (Exception e) {
			e.printStackTrace();
			TGlobal.g_log.WriteLogError("删除${comments}信息异常(" + e.getMessage() + "):" + sql);
		} 
		return result;
	}
	
	/**
	 * 修改${comments}<br>
	 */
	public int update${className}(${className}Bean ${classNameLower}Bean)
	{
		int result = -1;
		String sql = " update ${tableName} set ";
		<#list table.notKeyColumns as column>
			sql+="${column.sqlColumnName}="+${classNameLower}Bean.get${column.classColumnName?cap_first}()<#if column_has_next>+","</#if>;
		</#list>
		sql+=" where 1=1";
		<#list table.keyColumns as column>
				sql+=" and ${column.sqlColumnName}="+${classNameLower}Bean.get${column.classColumnName?cap_first}()<#if column_has_next>+","</#if>;
		</#list>
		try {
			result = TGlobal.g_dbcon.execUpdate(sql.toString());
			TGlobal.g_log.WriteLogDebug("修改${comments}信息:" + sql);
		}catch (Exception e) {
			e.printStackTrace();
			TGlobal.g_log.WriteLogError("修改${comments}信息异常(" + e.getMessage() + "):" + sql);
		} 
		return result;
	}
	
}